package com.gs.SynPatrolRep.presenter;

import com.google.gson.reflect.TypeToken;
import com.gs.SynPatrolRep.apiresult.ApiResultData;
import com.gs.SynPatrolRep.db.dto.CreatePalBillItemDto;
import com.gs.SynPatrolRep.db.dto.PalBillEventDto;
import com.gs.SynPatrolRep.db.dto.ShortDto;
import com.gs.SynPatrolRep.model.ComboModel;
import com.gs.SynPatrolRep.model.PalBillEventModel;
import com.gs.SynPatrolRep.model.UtilModel;
import com.gs.SynPatrolRep.model.listener.OnResultListener;
import com.gs.SynPatrolRep.view.ICreateReportView;

import java.io.IOException;
import java.util.List;

public class CreateReportPresenter extends BasePresenter<ICreateReportView> {
    private PalBillEventModel eventModel = new PalBillEventModel();
    private UtilModel utilsModel = new UtilModel();
    private ComboModel comboModel = new ComboModel();
    public void uploadImg(String img, int position) throws IOException {
        getView().showLoading();
        utilsModel.upload(img, new OnResultListener() {
            @Override
            public void onSuccess(Object aipResult) {
                if (getView() != null) {
                    getView().onUploadImgSuccess((ApiResultData<String>) aipResult,position);
                    getView().hideLoading();
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onUploadImgFails(e,position);
                    getView().hideLoading();
                }
            }
        },new TypeToken<ApiResultData<String>>(){}.getType());
    }

    public void combo(String typeKey) {
        getView().showLoading();
        comboModel.combo(typeKey,new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onGetComboSuccess((ApiResultData<List<ShortDto>>) apiResult);
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                    getView().onGetComboFails(e);
                }
            }
        }, new TypeToken<ApiResultData<List<ShortDto>>>() {
        }.getType());
    }

    public void create(CreatePalBillItemDto.Event bean) throws IOException {
        getView().showLoading();
        eventModel.create(bean, new OnResultListener() {
            @Override
            public void onSuccess(Object aipResult) {
                if (getView() != null) {
                    getView().onCreateSuccess((ApiResultData<String>) aipResult);
                    getView().hideLoading();
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onCreateFails(e);
                    getView().hideLoading();
                }
            }
        },new TypeToken<ApiResultData<String>>(){}.getType());
    }

    public void getData(String id) {
        getView().showLoading();
        eventModel.getData(id, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().onGetDataSuccess((ApiResultData<PalBillEventDto>) apiResult);
                    getView().hideLoading();
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onGetDataFails(e);
                    getView().hideLoading();
                }
            }
        }, new TypeToken<ApiResultData<PalBillEventDto>>() {
        }.getType());
    }
}
